package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.FocusUser;
import com.itheima.service.mongo.FocusUserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FocusUserServiceImpl implements FocusUserService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void save(FocusUser focusUser) {
        mongoTemplate.save(focusUser);
    }

    @Override
    public void delete(Long userId, Long focusUserId) {
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId).and("focusUserId").is(focusUserId)),FocusUser.class);
    }
    /**
     * 根据id查询粉丝
     * @param userId
     * @return
     */
    @Override
    public List<FocusUser> findAllById(Long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        List<FocusUser> focusUserList = mongoTemplate.find(query, FocusUser.class);
        return focusUserList;
    }

    @Override
    public FocusUser findById(String focusUserId) {
        Query query = new Query(
                Criteria.where("focusUserId").is(focusUserId)
        );
        return  mongoTemplate.findOne(query,FocusUser.class);
    }
}
